package com.ambow.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.entity.Product;
import com.ambow.service.ProductService;
import com.ambow.service.impl.ProductServiceImpl
;

/**
 * Servlet implementation class IndexServlet
 */
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String status = request.getParameter("status");
		if(status.equals("queryAll")) {
			queryAll(request,response);
		}
		
	}

	/**
	 * @param request
	 * @param response
	 *@author Administrator
	 * @throws IOException 
	 * @throws ServletException 
	 *@date 2017年12月24日 下午5:34:43
	 */
	private void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");
		String sql = "select * from Product where type='"+type+"'"+"order by rand()  limit 9";
		ProductService pService = new ProductServiceImpl();
		List<Product> list = pService.getRand(sql);
		if(list!=null&&!list.isEmpty()) {
			System.out.println("hhhhhhhhh成功");
			request.setAttribute("list", list);
			request.getRequestDispatcher("pages/product.jsp").forward(request, response);
		}else {
			System.out.println("kkkkkkkkkk失败");
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
	}

}
